package com.xgx.mp.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @description: mybatisplus元数据处理
 * @author: xgx
 * @create: 2019-09-17 14:43
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if(metaObject.hasSetter("createTime")){
            this.setInsertFieldValByName("createTime",LocalDateTime.now(),metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.hasSetter("updateTime")){
            LocalDateTime updateTime = (LocalDateTime)this.getFieldValByName("updateTime", metaObject);
            if(updateTime!=null){
                this.setUpdateFieldValByName("updateTime",updateTime,metaObject);
            }else{
                this.setUpdateFieldValByName("updateTime",LocalDateTime.now(),metaObject);
            }
        }

    }
}
